/*
Ubuntu is an Ubiquity command for searching Ubuntu related resources on specific
keyword search.

Ubuntu can be executed in Ubiquity by typing:
Ubuntu {search keywords}.

For Example:
Searching for VLC Media Player for Ubuntu: Ubuntu VLC Media Player 


Ubuntu has been tested with Ubiquity 0.5.4 with mozilla firefox 3.5.8 under Ubuntu Linux 9.10.
*/

var PREVIEW_TEMPLATE = "<a href='${href}'>${linkTitle}</a>";
var MORE_TEMPLATE= "<a href='${href}'><b>more...</b></a>"

var GOOGLE_SEARCH = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
var GOOGLE_SEARCH_BY_SITE = GOOGLE_SEARCH + "site:";

var GOOGLE_WEB_SEARCH = "http://www.google.com/search?q=";
var GOOGLE_WEB_SEARCH_BY_SITE = GOOGLE_WEB_SEARCH  + "site:";

function prepareTemplate(template,templateData)
{
	var previewTemplate = _(template);
	return  CmdUtils.renderTemplate(previewTemplate, templateData);
}


function search(params,previewBlock)
{	 
	var searchURL = params.searchService + params.withinSite + params.keyword;
	
	CmdUtils.previewGet(
			previewBlock, 			
			searchURL, 			
			function(data){
	   				                
				previewBlock.innerHTML += ("Search Result from <b> " + params.siteTitle + "</b>:<br/>");
       
				var searchResults = Utils.decodeJson(data).responseData.results;
        	        	        
				if(searchResults.length == 0)
				{
					previewBlock.innerHTML += "No Result <br/><br/>";
				}
        				       
				//show 3 search result from the particular site that we are searching
				for(var i = 0; i < 3 && searchResults.length; i++){
	        
					var previewContent = prepareTemplate(PREVIEW_TEMPLATE,
							                         {linkTitle: searchResults[i].title,
	        			                      		  href: searchResults[i].url});	        
					previewBlock.innerHTML += ((i+1) + ". " + previewContent + "<br/>");
				}
				
				var moreHREF = params.extraSearch + params.withinSite + params.keyword;
				
				previewBlock.innerHTML += prepareTemplate(MORE_TEMPLATE,{href:moreHREF});		
				previewBlock.innerHTML += "<br/><br/>";
					
			});
	
}
CmdUtils.CreateCommand({
	  names: ["ubuntu"],
	  icon: "http://www.google.com/favicon.ico",
	  homepage: "http://code.google.com/p/zainulfranciscusubiquity/",
	  author: { name: "zainul franciscus"},
	  license: "Creative Common Licence Attribution-Noncommercial-No Derivative Works 3.0 New Zealand (http://creativecommons.org/licenses/by-nc-nd/3.0/nz/)",
	  help: "Searches keyword search in popular ubuntu resources",
	  arguments: [{role:'object', nountype: noun_arb_text, label:'keywords'}],
	  preview: function(previewBlock, args) {
		  
		previewBlock.innerHTML = "";
		  
		var searchText = jQuery.trim(args.object.text);
		if(searchText.length < 1)
		{
			return;
		}
		
		search({searchService: GOOGLE_SEARCH_BY_SITE, withinSite: "http://www.ubuntugeek.com ",siteTitle:"Ubuntu Geek", keyword: searchText,extraSearch:GOOGLE_WEB_SEARCH_BY_SITE},previewBlock);				
		search({searchService: GOOGLE_SEARCH_BY_SITE, withinSite: "http://www.howtogeek.com ",siteTitle:"How To Geek", keyword: searchText,extraSearch:GOOGLE_WEB_SEARCH_BY_SITE},previewBlock);		
		search({searchService: GOOGLE_SEARCH_BY_SITE, withinSite: "http://ubuntuforums.org/ ",siteTitle:"Ubuntu Forum", keyword: searchText,extraSearch:GOOGLE_WEB_SEARCH_BY_SITE},previewBlock);
		search({searchService: GOOGLE_SEARCH_BY_SITE, withinSite: "https://launchpad.net ",siteTitle:"Ubuntu LaunchPad", keyword: searchText,extraSearch:GOOGLE_WEB_SEARCH_BY_SITE},previewBlock);			
		search({searchService: GOOGLE_SEARCH, withinSite: "",siteTitle:"General Search", keyword: searchText, extraSearch:GOOGLE_WEB_SEARCH},previewBlock);
						
	  }
});


